﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace LinqProviders.LinqToNaver
{
    /// <summary>
    /// Ranking Section 
    /// http://dev.naver.com/openapi/apis/contents/rank
    /// </summary>
    public enum RankMode
    {
        /// <summary>
        /// 통합검색
        /// </summary>
        nexearch,
        /// <summary>
        /// 카페검색
        /// </summary>
        cafe,
        /// <summary>
        /// 블로그 검색
        /// </summary>
        blog,
        /// <summary>
        /// 사전검색 
        /// </summary>
        dic,
        /// <summary>
        /// 이미지검색 
        /// </summary>
        image,
        /// <summary>
        /// 지식iN검색 
        /// </summary>
        kin,
        /// <summary>
        /// 음악검색 
        /// </summary>
        music,
        /// <summary>
        /// 동영상검색 
        /// </summary>
        video,
        /// <summary>
        /// 웹문서검색 
        /// </summary>
        webkr,
        /// <summary>
        /// 사이트검색 
        /// </summary>
        site,
        /// <summary>
        /// 뉴스검색
        /// </summary>
        news
    }

    /// <summary>
    /// query=movie 인물, 게임 등 특정 주제별 실시간 급상승 검색어를 조회합니다.
    /// </summary>
    public enum Theme
    {
        movie,
        schedulemovie,
        runningmovie,
        people,
        singer,
        foreignsinger,
        talent,
        actor,
        foreignactor,
        entertainer,
        broadcaster,
        comedian,
        sportsstar,
        soccer,
        foreignsoccer,
        drama,
        broadcast,
        game,
        perform,
        book,
        university
    }


    /// <summary>
    /// 실시간 급상승 검색어
    /// </summary>
    public class RankSearchResult : NaverSearchResult
    {
        /// <summary>
        /// Gets or sets the keyword. 검색 키워드.
        /// </summary>
        /// <value>The keyword.</value>
        public string Keyword { get; set; }
        /// <summary>
        /// Gets or sets the sign. 증감 기호 . +/-.
        /// </summary>
        /// <value>The sign.</value>
        public string Sign { get; set; }
        /// <summary>
        /// Gets or sets the delta. 순위 변동값.
        /// </summary>
        /// <value>The delta.</value>
        public int Delta { get; set; }
        /// <summary>
        /// Gets or sets the rating. 현재 순위.
        /// </summary>
        /// <value>The rating.</value>
        public int Rating { get; set; }

        /// <summary>
        /// Gets the channel query.
        /// </summary>
        /// <param name="xdoc">The xdoc.</param>
        /// <returns></returns>
        private IEnumerable<RankSearchResult> Parse(string input)
        {
            if (string.IsNullOrEmpty(input))
                return null;

            XDocument xdoc = XDocument.Parse(input);

            int rate = 1;

            IList<RankSearchResult> result = new List<RankSearchResult>();
                   var items = xdoc.Descendants("item").FirstOrDefault();

                   if (items == null)
                       return result;


                   for (int index = 1; index < 11; index++)
                   {
                       var rItem = items.Element("R" + rate.ToString())?? null;
                       if (rItem != null)
                       {

                           var rankItem = new RankSearchResult
                           {
                               Keyword = rItem.Element("K") != null ? rItem.Element("K").Value : "",
                               Sign = rItem.Element("S") != null ? rItem.Element("S").Value : "",
                               Delta = int.Parse(rItem.Element("V") != null ? rItem.Element("V").Value : "0"),
                               Rating = rate
                           };

                           rate++;

                           if (rankItem != null)
                               result.Add(rankItem);
                       } 
                   }
            return result;
            
        }

        protected override object Parse(string input, string requestedQuery, out int count, out int totalCount)
        {
            IEnumerable<RankSearchResult> resultList = new List<RankSearchResult>();
            count = 0;

            if (string.IsNullOrEmpty(input) == false)
            {
                resultList = Parse(input);
            }
            totalCount = 10;
            return resultList;
        }
    }

    
}
